package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.AttributeAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.AttributeListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeStandardVO;
import cn.tedu.csmall.product.service.AttributeService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理属性相关请求的控制器
 *
 * @author Chen
 * @since 2023/2/10 19:21:21
 */
@Slf4j
@RestController
@RequestMapping("/attributes")
@Tag(name = "05. 属性管理模块")
@Validated
public class AttributeController {

    private final AttributeService attributeService;

    @Autowired
    public AttributeController(AttributeService attributeService) {
        log.debug("创建控制器对象：AttributeController");
        this.attributeService = attributeService;
    }

    // http://localhost:8088/attributes/add-new
    @ApiOperationSupport(order = 100)
    @Operation(summary = "添加属性")
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Valid AttributeAddNewDTO attributeAddNewDTO) {
        attributeService.addNew(attributeAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:8088/attributes/9527/delete
    @Parameter(name = "id", description = "属性ID", required = true, example = "9527",
            schema = @Schema(type = "integer", format = "int64"))
    @ApiOperationSupport(order = 200)
    @Operation(summary = "根据id删除属性")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1L, max = 10000L, message = "删除属性失败，属性ID非法！") @PathVariable Long id) {
        log.debug("开始处理【根据id删除属性】的请求，参数：{}", id);
        attributeService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:8088/attributes/9527/update
    @ApiOperationSupport(order = 300)
    @Operation(summary = "根据id修改属性")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> update(@PathVariable Long id, AttributeUpdateDTO attributeUpdateDTO) {
        log.debug("开始处理【根据id修改属性详情】的请求，参数ID：{}，新数据：{}", id, attributeUpdateDTO);
        attributeService.updateInfoById(id, attributeUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:8088/attributes/9527
    @Parameter(name = "id", description = "属性ID", required = true, example = "9527",
            schema = @Schema(type = "integer", format = "int64"))
    @ApiOperationSupport(order = 410)
    @Operation(summary = "根据id查询属性详情")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<AttributeStandardVO> getStandardById(@Range(min = 1L, max = 10000L, message = "查询属性详情失败，属性ID非法！") @PathVariable Long id) {
        log.debug("开始处理【根据id查询相册详情】的请求，参数：{}", id);
        AttributeStandardVO attribute = attributeService.getStandardById(id);
        return JsonResult.ok(attribute);
    }

    // http://localhost:8088/attributes
    @ApiOperationSupport(order = 420)
    @Operation(summary = "查询属性列表")
    @GetMapping("")
    public JsonResult<List<AttributeListItemVO>> list() {
        log.debug("开始处理【查询属性列表】的业务，无参数");
        List<AttributeListItemVO> list = attributeService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:8088/attributes/list-by-template?templateId=9527
    @Parameter(name = "templateId", description = "属性模板ID", required = true,
            schema = @Schema(type = "integer", format = "int64"))
    @ApiOperationSupport(order = 421)
    @Operation(summary = "根据属性模板查询属性列表")
    @GetMapping("/list-by-template")
    public JsonResult<List<AttributeListItemVO>> listByTemplate(Long templateId) {
        log.debug("开始处理【根据属性模板查询属性列表】的业务，无参数");
        List<AttributeListItemVO> list = attributeService.listByTemplate(templateId);
        return JsonResult.ok(list);
    }

}
